<?php

use yii\helpers\Html;
use yii\helpers\Url;
use frontend\widgets\hot\HotWidget;
use yii\base\Widget;
use frontend\widgets\tag\TagWidget;

/** @var $data frontend\models\Article */
/** @var $post common\models\ArticleModel */
/** @var $comments common\models\Comment */
/** @var $replys common\models\Reply */
/** @var $username common\models\User */

$this->title = $data['title'];
$this->params['breadcrumbs'][] = [
    'label' => '文章',
    'url' => [
        'article/index'
    ]
];
$this->params['breadcrumbs'][] = $this->title;

?>

<div class="row">
    <div class="col-lg-9">
        <div class="page-title">
            <h1><?= $data['title'] ?></h1>
            <span>分类：<?= $data['cat']['cat_name'] ?></span>
            <span>作者：<?= $data['user_name'] ?></span> <span>发布于：<?= date('Y-m-d', $data['created_at']) ?></span>

            <span>浏览：<?= isset($data['extend']['browser']) ? $data['extend']['browser'] : 0 ?>次</span>

        </div>

        <div class="page-content">
            <?= $data['content'] ?>
        </div>

        <div class="tags">
            标签：
            <?php if (is_array($data['tags'])): ?>
                <?php foreach ($data['tags'] as $tag): ?>
                    <span><a href="#"><?= $tag ?></a></span>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

    </div>


    <div class="col-lg-3">
        <div class="panel">
            <?php if (!\Yii::$app->user->isGuest): ?>
                <a class="btn btn-success btn-block btn-post" href="<?= \yii\helpers\Url::to(['article/create']) ?>">创建文章</a>
                <?php if (\Yii::$app->user->identity->id == $data['user_id']): ?>
                    <a class="btn btn-info btn-block btn-post"
                       href="<?= Url::to(['article/update', 'id' => $data['id']]) ?>">编辑文章</a>
                <?php endif; ?>
            <?php endif; ?>
        </div>
        <?= HotWidget::widget() ?>
        <?= TagWidget::widget() ?>
    </div>

    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading"><b>评论</b></div>
            <div class="panel-body">
                <ul class="media-list">
                    <?php $i = 1;
                    foreach ($comments as $comment):   //循环输出所有评论
                        ?>
                        <li class="media">
                            <div class="media-body">
                                <h4 class="media-heading"><?php echo '第' . $i . '楼:' . "评论于" . date('Y-m-d H:i:s', $comment->created_at); ?></h4>
                                <p class="media-content"><?php echo yii\helpers\Markdown::process($comment->comment, 'gfm'); ?></p>
                            </div>
                        </li>
                        <?php
                        if (($replyes = $replys[$comment->id]) != false) {
//             					echo "<hr>";
                            foreach ($replyes as $reply) {                 //如果评论有回复，循环输出所有回复
                                ?>
                                <li class="media">
                                    <div class="media-body">
                                        <div class="media-reply">
                                            <?= "回复于" . date('Y-m-d H:i:s', $reply->created_at); ?>
                                        </div>
                                        <div class="media-content">
                                            <div class="media-reply">
                                                <?php echo yii\helpers\Markdown::process($reply->reply, 'gfm'); ?>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            <?php }
                        } ?>
                        <a href="javascript:void(0)" onclick="reply(this,<?php echo $comment->id; ?>)"
                           style="margin-left:30px;">回复</a>
                        <hr>
                        <hr>

                        <?php $i++; endforeach; ?>
                </ul>
            </div>
        </div>


        <!-- 回复评论 -->
        <?= Html::beginForm(['comment/add', 'id' => $post->id], 'post') ?>
        <h4>输入评论内容</h4>
        <?= \yidashi\markdown\Markdown::widget(['name' => 'comment', 'language' => 'zh']) ?>
        <br>
        <?= Html::submitButton('提交', ['class' => 'btn btn-primary']) ?>

        <?= Html::endForm() ?>


        <?php //回复表单 ?>
        <div class="hidden" id="reply" style="margin-left:30px;">
            <br>
            <?= Html::beginForm(['reply/add'], 'post') ?>
            <?= Html::hiddenInput('comment_id', 0, ['id' => 'comment_id']); ?>
            <?= \yidashi\markdown\Markdown::widget(['name' => 'reply', 'language' => 'zh']) ?>
            <br>
            <?= Html::submitButton('提交', ['class' => 'btn btn-primary']) ?>

            <?= Html::endForm() ?>
        </div>


        <script type="text/javascript">
            function reply(reply, comment_id) {           //显示回复内容输入框，参数为该元素，和评论id
                $('#comment_id').val(comment_id);  //修该表单  comment_id 的值为该评论的 id
                $('#reply').removeClass('hidden');   //显示回复输入框
                $(reply).after($('#reply'));  //将回复框移到该元素之后

            }
        </script>

    </div>

</div>
